var fs = require('fs');
var analysisLog = require('./analysisLog');
process.stdin.setEncoding('utf8');

var inputDir = 'data';
fs.readdir(inputDir, function(err, paths) {
    //  console.log("arguments", arguments);
    paths
        .forEach(function(fileName) {
            fileName = inputDir + '/' + fileName;
            fs.stat(fileName, function(err, stats) {
                if(err) {
                    throw err;
                }
                if(stats.isFile()) {
                    analysisFile(fileName);
                }
            });
        });
})
var analysisFile = function(fileName) {
    var log = fs.readFileSync(fileName, 'utf-8');
    //console.log("log",log);
    var res = analysisLog(log);
    //  process.stdout.write(res + "\n");    
    var outputDir = "output/";
    var fn = fileName.split("/").pop();
    var outputPath = outputDir + fn.replace(/\.log$/, '.txt');
    fs.stat(outputDir, function(err, stats) {
        if(err) {
            console.log("err", err);
            fs.mkdir(outputDir, function() {
                fs.writeFileSync(outputPath, res);
            });
        } else if(stats.isDirectory()) {
            fs.writeFileSync(outputPath, res);
        }
    });
};